package study;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.apache.http.client.HttpClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import study.config.HttpClientProperties;

import java.util.List;
import java.util.Map;

/**
 * 方式：在Controller中加@HystrixCommand
 * http://127.0.0.1:8082/actuator/hystrix.stream
 *
 *
 * 注：执行fallback不一定circuitBreaker open
 * 	执行fallback的条件：a、circuitBreaker open  b、threadPool满了  c、hystrix从command中获取结果onError或抛异常
 * 	circuitBreaker open条件：a、qps高  b、错误百分比高（有个天条件是5s内失败20次）
 */
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class RestTemplateApp {
	

	public static void git (String[] args) {
		SpringApplication application = new SpringApplication(RestTemplateApp.class);
		ConfigurableApplicationContext context = application.run(args);


		System.out.println("***************打印消息***************");
		System.out.println("***************打印消息***************");
		System.out.println("***************打印消息***************");


        Map<String, HttpClientProperties> httpClientPropertiesMap = context.getBeansOfType(HttpClientProperties.class);
        System.out.println("httpClientPropertiesMap::" + httpClientPropertiesMap);
        Map<String, HttpClient> HttpClient = context.getBeansOfType(HttpClient.class);
        System.out.println("httpClientConfigMap::" + HttpClient);
		Map<String, RestTemplate> restTemplateMap = context.getBeansOfType(RestTemplate.class);
		System.out.println("restTemplateMap::" + restTemplateMap);
		System.out.println("***************打印消息***************");
		RestTemplate restTemplate = (RestTemplate)context.getBean("restTemplate");
		System.out.println(restTemplate.getMessageConverters());
		List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
		for (HttpMessageConverter httpMessageConverter: messageConverters) {
			if (httpMessageConverter.getClass() == FastJsonHttpMessageConverter.class) {
				FastJsonHttpMessageConverter fa = (FastJsonHttpMessageConverter)httpMessageConverter;
				System.out.println(fa.getSupportedMediaTypes());
			}
		}


		System.out.println("======================RestTemplateApp：end=======================");
	}

}
